﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using DevExpress.XtraEditors;

using CRM.GUI.QLKhachHang;
using DTO;
using BLL;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmBasicList : DevExpress.XtraEditors.XtraForm
    {
        public frmBasicList()
        {
            InitializeComponent();
            grdViewBasicList.BestFitColumns();
            grdViewBasicList.OptionsView.ColumnAutoWidth = false;
            grdViewBasicList.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewBasicList.HorzScrollVisibility = ScrollVisibility.Always;
            IntialLoad();
            grdNo.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewBasicList.CustomUnboundColumnData += grdViewBasicList_CustomUnboundColumnData;
        }
        ExcelFunc import;
        private List<CustomerEntity> list = new List<CustomerEntity>();

        private DataTable _storage;
        public static LoadDup LoadDupList;
        public delegate void LoadDup(DataTable dtDupList);

        private void IntialLoad()
        {
            
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
            cmbCity.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCity.DisplayMember = "RegionName";
            cmbCity.ValueMember = "Region_Code";
            cmbCollector.DataSource = SharedData.CollectorList;
            cmbCollector.DisplayMember = "Collector_Code";
            cmbCollector.ValueMember = "Collector_ID";
            cmbCurrentBrand.DataSource = SharedData.BrandList;
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";
            cmbPreviousBrand.DataSource = SharedData.BrandList;
            cmbPreviousBrand.DisplayMember = "Brand_Code";
            cmbPreviousBrand.ValueMember = "Brand_ID";
            grdBasicList.UseDisabledStatePainter = true;
        }

        void grdViewBasicList_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }


        private void mbtnImport_Click(object sender, EventArgs e)
        {
            ExcelFunc.LoadDataToUI = new ExcelFunc.OnLoadComplete(LoadToGridView);
            frmImport f = new frmImport();
            f.ShowDialog();
            _storage = grdBasicList.DataSource as DataTable;
        }

        private void LoadToGridView(DataTable dt)
        { 
            import = new ExcelFunc();
            grdBasicList.DataSource = dt;
        }

        #region Show/Hide List
        private void mItemSource_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemSource.CheckState == CheckState.Checked)
            {
                grdBaSource.Visible = true;
            }
            else
            {
                grdBaSource.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemMother_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemMother.CheckState == CheckState.Checked)
            {
                grdBaMother.Visible = true;
            }
            else
            {
                grdBaMother.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemAddress_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemAddress.CheckState == CheckState.Checked)
            {
                grdBaAdress.Visible = true;
            }
            else
            {
                grdBaAdress.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemEDD_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemEDD.CheckState == CheckState.Checked)
            {
                grdBaEDD.Visible = true;
            }
            else
            {
                grdBaEDD.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mItemChild_CheckStateChanged(object sender, EventArgs e)
        {
            if (mItemChild.CheckState == CheckState.Checked)
            {
                grdBaChild.Visible = true;
            }
            else
            {
                grdBaChild.Visible = false;
            }
            mbtnDetail_Click(sender, e);
        }

        private void mbtnDetail_Click(object sender, EventArgs e)
        {
            mbtnDetail.ShowDropDown();
        }
        #endregion

                
        private void mbtnFilter_Click(object sender, EventArgs e)
        {
            frmDuplicateReport f = new frmDuplicateReport();
            FilterFunc filter = new FilterFunc();
            if ((grdBasicList.DataSource as DataTable) != null && (grdBasicList.DataSource as DataTable).Rows.Count > 0)
            {
                filter.FilterDupList(filter.dtRaw);
                if (filter.DupList != null)
                {
                    f.lblTotalListCount.Text = filter.dtRaw.Rows.Count.ToString();
                    LoadDupList(filter.DupList);
                    f.ShowDialog();
                    btnUpLoad.Enabled = true;
                }                
            }
            else
            {
                MessageBox.Show("Chưa có dữ liệu để lọc", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpLoad_Click(object sender, EventArgs e)
        {
            ImportFunc import = new ImportFunc();
            DataTable table = (grdBasicList.DataSource as DataTable);
            DataTranferEntity trans = new DataTranferEntity();
            if (table != null && table.Rows.Count > 0)
            {
                try
                {
                    import.ImportCustomer(table, trans.Source);
                    MessageBox.Show("Upload dữ liệu lên server thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {                    
                    MessageBox.Show("Upload dữ liệu lên server thất bại\nXin xem lại dữ liệu cần upload hoặc đường truyền mạng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);                    
                }
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để upload", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            btnUpLoad.Enabled = false;
        }

        

    }
}